package com.wlhse.service.impl;

import com.wlhse.dao.CompanyDao;
import com.wlhse.dao.CompanyTreeDao;
import com.wlhse.entity.CompanyPojo;
import com.wlhse.entity.CompanyStructChange;
import com.wlhse.exception.WLHSException;
import com.wlhse.service.CompanyTreeService;
import com.wlhse.util.SortCodeUtil;
import com.wlhse.util.state_code.CodeDict;
import com.wlhse.util.state_code.NR;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Service
public class CompanyTreeServiceImpl implements CompanyTreeService {
    @Resource
    private CompanyTreeDao companyTreeDao;

    @Resource
    private SortCodeUtil sortCodeUtil;

    @Resource
    private CompanyDao companyDao;

    @Override
    @Transactional
    public String addCompanyTreeNode(CompanyPojo companyPojo) {
        //可以使用Validator
        if (StringUtils.isBlank(companyPojo.getName()))
            throw new WLHSException("输入为空");
//        if (companyDao.getNameCount(companyPojo.getName()) > 0)
//            throw new WLHSException("单位已存在");
        String parent = companyPojo.getCompanyCode();
        String code = companyTreeDao.queryChildCode(parent);
        if (StringUtils.isNotBlank(code))
//            companyPojo.setCompanyCode(sortCodeUtil.next(code));
            companyPojo.setCompanyCode(sortCodeUtil.nextCode(code));
        else
            companyPojo.setCompanyCode(parent + "01");

        //新增company的order默认为同层最大
        List<CompanyPojo> companyPojos = companyDao.querySamleLevelCompany(companyPojo.getCompanyCode());//查询同级公司
        Integer max=0;
        for (CompanyPojo pojo : companyPojos) {
            if (pojo.getOrder()>max){
                max=pojo.getOrder();
            }
        }
        companyPojo.setOrder(max+1);

        if (companyTreeDao.addCompanyTreeNode(companyPojo) <= 0)
            throw new WLHSException("新增失败");
        return NR.r();
    }

    @Override
    @Transactional
    public String deleteCompany(String companyCode) {
        if (companyTreeDao.deleteCompanyTreeNode(companyCode) <= 0)
            throw new WLHSException("删除失败");
        return NR.r();
    }

    @Override
    @Transactional
    public String updateCompany(CompanyPojo companyPojo) {
        if (companyTreeDao.updateCompanyTreeNode(companyPojo) <= 0)
            throw new WLHSException("更新失败");
        return NR.r();
    }

    @Override
    @Transactional
    public String updateCompanyName(CompanyPojo companyPojo) {
        companyDao.updateCompanyName1(companyPojo.getName(),companyPojo.getCompanyCode());
        companyDao.updateCompanyName2(companyPojo.getName(),companyPojo.getCompanyCode());
        companyDao.updateCompanyName3(companyPojo.getName(),companyPojo.getCompanyCode());
        return NR.r();
    }

    @Override
    @Transactional
    public String updateCompanyStruct(CompanyStructChange companyStructChange) {
        String parentCode = companyStructChange.getParentCode();
        List<String> changeCodes = companyStructChange.getChangeCode();
        Integer checkCounts = companyDao.checkChangedCompany(changeCodes);
        if(checkCounts!=changeCodes.size()){//检查修改公司
            return NR.r(CodeDict.UPDATE_FAILE_CASE,"修改公司不存在");
        }

        String code = companyTreeDao.queryChildCode(parentCode);
        Integer order = companyTreeDao.queryMaxOrder(parentCode);
        String nextCode="";
        if (StringUtils.isNotBlank(code)&&code!=null) {
            nextCode = sortCodeUtil.nextCode(code);
        } else{
            nextCode=parentCode+"01";
        }
        if(order==null){
            order=1;
        }else {
            order+=1;
        }
        LinkedHashMap<String, String> companyCodes = new LinkedHashMap<>();
        List<CompanyPojo> companyPojos = companyDao.queryCompanyList(changeCodes);
        for (CompanyPojo companyPojo : companyPojos) {//修改company
            companyCodes.put(companyPojo.getCompanyCode(),nextCode);//新旧code对应关系
            companyPojo.setOrder(order);
            companyPojo.setCompanyCode(nextCode);
            companyTreeDao.addCompanyTreeNode(companyPojo);
            nextCode = sortCodeUtil.nextCode(nextCode);
            order+=1;
        }

        for (Map.Entry<String, String> companyCodeEntry : companyCodes.entrySet()) {//修改关联表的记录，并删除之前的company
            String oldCode = companyCodeEntry.getKey();
            String newCode = companyCodeEntry.getValue();
            companyDao.updateEmployeeComCode(oldCode,newCode);
            companyDao.updateSupervisionAreaComCode(oldCode,newCode);
            companyDao.updateSupervisionDetail2ComCode(oldCode,newCode);
            companyDao.updateSupervisionScopeComCode(oldCode,newCode);
            companyTreeDao.deleteCompanyByCode(oldCode);
        }


        return NR.r();
    }
}
